package com.woniuxy.dao;

import com.woniuxy.dto.*;
import com.woniuxy.pojo.Property;
import com.woniuxy.pojo.Users;
import com.woniuxy.pojo.Vendor;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface PropertyMapper {
    int deleteByPrimaryKey(Long id);

    int insert(Property record);

    int insertSelective(Property record);

    Property selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Property record);

    int updateByPrimaryKey(Property record);

    @Select("select * from property where account = #{account}")
    Property findProByAccount(String account);

    @Insert("insert into property (account,pass_word) values (#{account},#{passWord})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void saveProResultVid(Property v);


    @Select("<script>" +
            "SELECT ss.id  id,u.user_name  username,u.phone_number phone,sm.server_name servername,ss.subscribe_time subtime FROM property p\n" +
            "LEFT JOIN servers s\n" +
            "ON p.id = s.supplier_id\n" +
            "LEFT JOIN server_menu sm\n" +
            "ON s.server_menu_id = sm.id\n" +
            "LEFT JOIN server_subscribe ss\n" +
            "ON s.id = ss.server_id\n" +
            "LEFT JOIN users u\n" +
            "ON ss.user_id = u.id\n" +
            "WHERE p.id = #{pid} AND s.server_type = 0  AND ss.status = 1" +
            "<if test = 'name != null'>" +
            "AND u.user_name like  concat('%',#{name},'%') " +
            "</if>" +
            "</script> ")
        //s.server_type = 0,代表该服务为商家入驻，ss.status = 1，代表改预约没有被接取
    List<ResultAllService> findAllServiceByVid(@Param("pid") Long pid ,@Param("name") String name,@Param("currentPage") Integer currentPage,@Param("pageSize") Integer pageSize);

    @Update("UPDATE server_subscribe SET `status` = 0 WHERE id = #{ssid}")
    void updateSubscribeStatus(Long ssid);

    @Select("SELECT ss.id ssid,s.price,ss.user_id FROM server_subscribe ss\n" +
            "LEFT JOIN servers s\n" +
            "ON ss.server_id = s.id\n" +
            "WHERE ss.id = #{id}")
    MiddleData findMiddleData(Long ssid);


    @Select("SELECT so.id,so.order_num sonum,sm.server_name servername,u.user_name username, \n" +
            "u.phone_number phone,s.price,ss.subscribe_time sstime,so.create_time createtime FROM property p \n" +
            "LEFT JOIN servers s \n" +
            "ON p.id = s.supplier_id \n" +
            "LEFT JOIN server_menu sm \n" +
            "ON sm.id = s.server_menu_id \n" +
            "LEFT JOIN server_subscribe ss \n" +
            "ON ss.server_id = s.id \n" +
            "LEFT JOIN server_orders so \n" +
            "ON so.ser_id = ss.id \n" +
            "LEFT JOIN users u \n" +
            "ON u.id = ss.user_id \n" +
            "WHERE p.id =#{id} AND s.server_type = 0 AND ss.status = 0 AND so.pay_status = 0")
    //s.server_type = 0表示物业的注册的服务，ss.status = 0表示预约已被接取后生成的订单
    //so.pay_status = 0表示订单未完成或用户为点击确认完成服务
    List<ResultHasServer> findHasServerByPropertyId(Long id);

    @Update("UPDATE property SET balance = balance + #{price} WHERE id = #{pid}")
    void updateBalanceByPid(@Param("pid") Long pid,@Param("price") Double price);

    @Select("SELECT so.id ssid,so.order_num sonum,sm.server_name servername,u.user_name username,u.phone_number phone,so.price,so.end_time endtime \n" +
            " FROM property p\n" +
            "LEFT JOIN servers s\n" +
            "ON p.id = s.supplier_id\n" +
            "LEFT JOIN server_menu sm\n" +
            "ON sm.id = s.server_menu_id\n" +
            "LEFT JOIN server_subscribe ss\n" +
            "ON ss.server_id = s.id\n" +
            "LEFT JOIN users u\n" +
            "ON ss.user_id = u.id\n" +
            "LEFT JOIN server_orders so\n" +
            "ON so.ser_id = ss.id\n" +
            "WHERE p.id = #{pid} AND so.pay_status = 3")
    List<ResultFinishedSercer> findFinishServerOrder(Long pid);

    @Select("select * from property where id = #{id}")
    Property selectByPid(Long pid);

    @Update("update property set pass_word = #{newPass} where id = #{id}")
    void updatePassById(@Param("id") Long id,@Param("newPass") String newPass);


    @Select("SELECT sm.id,sm.server_name sname,sm.description,s.price,s.enter_status status,s.enter_time stime FROM server_menu sm \n" +
            "LEFT JOIN servers s\n" +
            "ON sm.id = s.server_menu_id\n" +
            "WHERE sm.parent_id != 0 AND s.server_type = 0 AND s.supplier_id =#{id}")
    List<ResultServiceIn> findServiceIn(Long id);

    @Delete("DELETE FROM servers WHERE server_menu_id = #{smid} AND supplier_id = #{id} AND server_type = 0")
    void delServiceIn(@Param("id") Long id,@Param("smid") Long smid);

    @Update("UPDATE servers SET price = #{price} WHERE server_menu_id = #{smid} AND supplier_id = #{id} AND server_type = 0")
    void updateServerPrice(@Param("id") Long id,@Param("smid") Long smid,@Param("price") Double price);

    @Select("SELECT * FROM \n" +
            "(SELECT sm.id,sm.server_name sname,sm.description FROM server_menu sm WHERE sm.parent_id != 0) temp\n" +
            "WHERE temp.id NOT IN (SELECT s.server_menu_id id FROM servers s WHERE s.supplier_id = #{id} AND s.server_type = 0)")
    List<ResultServiceIn> canBeIn(Long id);

    @Insert("INSERT INTO servers(server_menu_id,supplier_id,price,server_type,enter_time,enter_status) VALUES(#{smid},#{id},#{price},0,#{date},0)")
    void oneServiceIn(@Param("id") Long id, @Param("smid") Long smid, @Param("price") Double price, @Param("date") Date date);


    @Select("select * from users where plot_id = #{id}")
    List<Users> ownerList(Long id);

    @Update("UPDATE users SET STATUS = #{status} WHERE id =#{id}")
    void checkUserStatus(@Param("id") Long id, @Param("status") Integer status);
}